/* excel导入 */
import React, { PureComponent } from 'react';
import { Cascader, Checkbox, Form, Upload, Button, message, Modal } from 'antd'
import { batchUploadQuestion } from '@/services/questionsBank'
import { baseurl } from "@/services/baseurl";
import './index.less'
const layout = {
    labelCol: { span: 2 },
    wrapperCol: { span: 16 },
};
const tailLayout = {
    wrapperCol: { offset: 2, span: 16 },
};
export default class Index extends PureComponent<any, any> {
    constructor(props: any) {
        super(props);
        this.state = {
            fileList: [],
            types: [],
            subject: [],
            loading: false
        }
    }
    changeTypes = (e: any) => {
        this.setState({
            types: e.sort()
        })
    }
    changeSubject = (e: any) => {
        this.setState({
            subject: e
        })
    }
    changeFiles = (info: any) => {
        const fileList = info.fileList;
        let file = null;
        if (fileList.length > 1) {
            file = [fileList[fileList.length - 1]];
        } else {
            file = fileList
        }
        this.setState({
            fileList: file
        })
    }
    submitHandle = () => {
        const { subject, types, fileList } = this.state;
        if (!types[0]) return Modal.error({
            title: '提示',
            content: '还未选择所属题库'
        })
        if (!subject[0]) return Modal.error({
            title: '提示',
            content: '还未选择所属科目'
        })
        if (!fileList[0]) return Modal.error({
            title: '提示',
            content: '还未选择文件'
        })
        this.setState({
            loading: true
        })
        batchUploadQuestion({ subject, types, doc: fileList[0].originFileObj }).then(res => {
            this.setState({
                loading: false
            })
            if (res.static === 1) {
                Modal.success({
                    title: '提示',
                    content: '上传成功'
                })
            } else {
                Modal.error({
                    title: '提示',
                    content: '上传失败'
                })
            }
        })
    }
    render() {
        const { subjectOptions } = this.props;
        const { fileList, loading } = this.state;
        return (
            <div>
                <Form>
                    <Form.Item {...layout} label="所属题库">
                        <Checkbox.Group style={{ width: '100%' }} onChange={this.changeTypes} >
                            <Checkbox key="3" value="3">模拟题库</Checkbox>
                            <Checkbox key="2" value="2">练习题库</Checkbox>
                            <Checkbox key="1" value="1">正式题库</Checkbox>
                            {/* <Checkbox key="4" value="4">每日一题</Checkbox> */}
                        </Checkbox.Group>
                    </Form.Item>
                    <Form.Item {...layout} label="所属科目">
                        <Cascader style={{ width: 400}}  options={subjectOptions} onChange={this.changeSubject} />
                    </Form.Item>
                    <Form.Item {...layout} label="上传文件">
                        <Upload accept=".xlsx,.xls" disabled={loading} beforeUpload={()=>{return false}} onChange={this.changeFiles} fileList={fileList}>
                            <Button type="primary">上传文件</Button>
                        </Upload>
                        <a href={baseurl + "Rubricxls.xls"} style={{ color: "red", marginTop: 20, display: 'block' }} >模板下载</a>
                    </Form.Item>
                    <Form.Item  {...tailLayout}><Button type="primary" loading={loading} onClick={this.submitHandle}>{loading ? '上传中' : '提交'}</Button></Form.Item>
                </Form>


            </div>
        )
    }
}
